package com.designpattern.singleton;

/**
 * @Classname SingletonTest
 * @Description 单例模式验证
 * @Date 2019/5/11 23:20
 * @Created by xiangty
 */
public class SingletonTest {

    public static void main(String[] args) {
        method1();

        method2();
    }

    /**
     * 验证饿汉模式
     */
    public static void method1(){
        Singleton1 singleton1 = Singleton1.getInstance();
        Singleton1 singleton2 = Singleton1.getInstance();

        if(singleton1 == singleton2){
            System.out.println("饿汉模式:singleton1和singleton2是同一个实例");
        } else {
            System.out.println("饿汉模式:singleton1和singleton2不是同一个实例");
        }
    }

    /**
     * 验证懒汉模式
     */
    public static void method2(){
        Singleton2 singleton1 = Singleton2.getInstance();
        Singleton2 singleton2 = Singleton2.getInstance();

        if(singleton1 == singleton2){
            System.out.println("懒汉模式:singleton1和singleton2是同一个实例");
        } else {
            System.out.println("懒汉模式:singleton1和singleton2不是同一个实例");
        }
    }

}
